<template>
  <div class='search'>
    <a-card size='small' v-if='showForm'>
      <div v-if='generateTaskFormVisible'>
        <k-form-build ref='taskForm' :config='config' :value='taskFormJson' :default-value='taskFormData' :dynamicData='dynamicData' />
      </div>
      <div v-if='generateStartFormVisible&&!generateTaskFormVisible'>
        <k-form-build ref='startForm' :value='startFormJson' :default-value='processInstanceFormData' :disabled='true' />
      </div>
    </a-card>
    <a-card :hoverable='true' style='margin-top:10px' size='small'>
      <f-form ref='form' :action='api.tasktodo.complete' :form-key='this.completeForm.taskId'>
        <f-form-item label='审批意见' type='textarea' name='message' :value.sync='message' :required='true' />
        <div style='margin-top: 5px;margin-left: 120px'>
          <a-button type='primary' @click='doAgree'>同意</a-button>
          <a-button type='default' @click='doBack' style='margin-left: 7px'>退回</a-button>&nbsp;
          <!--<a-button type="default" @click="">转办</a-button>&nbsp;
          <a-button type="default" @click="">委派</a-button>-->&nbsp;
        </div>
      </f-form>
    </a-card>
    <taskback-model ref='taskBackModel' v-if='dialogTaskBackNodesVisible' :visible.sync='dialogTaskBackNodesVisible' :executeTaskId='completeForm.taskId' @backTaskSubmit='backTaskSubmit'></taskback-model>
  </div>
</template>
<script>
import api from "./js/api"
import TaskbackModel from "./taskback_model"
import Vue from "vue"

export default {
  name: "taskTodoForm",
  props: {
    handlerName: {
      type: String,
      default: ""
    },
    taskHandlerName: {
      type: String,
      default: ""
    }
  },
  data() {
    const token = Vue.ls.get("Access-Token")
    return {
      refs: this.$refs,
      config: {
        uploadImage: "/base/file/design/form/upload", // 上传图片地址
        uploadImageName: "file", // 上传文件name
        uploadImageData: { busType: "BusPayment", busId: "" }, // 上传图片额外参数
        uploadImageHeaders: { Authorization: token } // 上传图片请求头部
      },
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      loaded: false,//是否加载完成
      completeForm: this.getFormItem(),
      activeName: "processInstanceForm",
      startFormKey: undefined,
      taskFormKey: undefined,
      startFormJson: undefined,
      showForm: false,
      taskFormJson: undefined,
      completeTaskHandler: "",
      generateStartFormVisible: false,
      generateTaskFormVisible: false,
      taskFormData: undefined,
      businessKey: undefined,
      processInstanceFormData: undefined,
      startUserId: "",
      isInitiator: false,
      selectUserVisible: false,
      selectUserType: "",
      message: "同意",
      dialogTaskBackNodesVisible: false,
      dynamicData: {
        bankList: []
      }
    }
  },
  methods: {
    getFormItem() {
      return {
        taskId: ""
      }
    },
    //表单赋值
    setData(data) {
      if (data && data.taskId) {
        this.completeForm.taskId = data.taskId
        this.http.get(this, {
          url: this.api.tasktodo.getTaskData,
          params: {
            taskId: data.taskId,
            handlerName: this.handlerName
          },
          success: function(me, data) {
            me.startUserId = data.startUserId
            me.businessKey = data.businessKey
            me.config.uploadImageData.busId = data.businessKey
            me.startFormKey = data.startFormKey
            me.taskFormKey = data.taskFormKey
            me.isInitiator = data.isInitiator
            me.showForm = false
            if (data.startFormJson) {
              me.startFormJson = JSON.parse(data.startFormJson)
              me.processInstanceFormData = JSON.parse(data.variables.processInstanceFormData)
              me.generateStartFormVisible = true
              me.showForm = true
            }
            if (data.taskFormJson) {
              me.taskFormJson = JSON.parse(data.taskFormJson)
              if (me.taskFormKey && data.taskFormData) {
                me.taskFormData = data.taskFormData
              }
              me.showForm = true
              me.completeTaskHandler = data.completeTaskHandler
              me.generateTaskFormVisible = true
            }
          }
        })
      }
    },
    //同意
    doAgree(callback) {
      if (this.taskHandlerName) {
        this.completeTaskHandler = this.taskHandlerName
      }
      let params = {
        taskId: this.completeForm.taskId,
        message: this.message,
        completeTaskHandler: this.completeTaskHandler
      }
      if (this.$refs.taskForm) {
        params.isInitiator = this.isInitiator
        //任务表单不为空
        this.$refs["taskForm"].getData().then(values => {
          if (values && values != undefined) {
            let realValues = values
            if (this.startFormKey && this.taskFormKey && this.startFormKey == this.taskFormKey) {
              let processInstanceFormData = JSON.stringify(values)
              realValues = Object.assign({ processInstanceFormData }, values)
            }
            params.formMap = realValues
            this.$refs.form.handlerSubmit(params, this.closeDrawer, true)
          }
        })
      } else {
        this.$refs.form.handlerSubmit(params, this.closeDrawer, true)
      }
    },
    //关闭侧弹
    closeDrawer() {
      this.$emit("callback", true)
    },
    //打开退回节点选择框
    doBack() {
      this.$refs.form.getForm().validateFields((valid, values) => {
        if (!valid) {
          this.dialogTaskBackNodesVisible = true
        }
      })
    },
    /**
     * 获取支付银行
     */
    getBankList() {
      this.http.get(this, {
        url: this.api.bank.list,
        params: {},
        success: function(me, data) {
          data.map(item => {
            item.label = item.bankName + "-" + item.bankNo
            item.value = item.bankId
          })
          me.dynamicData.bankList = data
        }
      })
    },
    //执行退回
    backTaskSubmit(backNode, message) {
      if (message) {
        this.message = message
      }
      this.http.post(this, {
        url: this.api.tasktodo.backTask,
        data: {
          taskId: this.completeForm.taskId,
          activityId: backNode.nodeId,
          activityName: backNode.nodeName,
          message: this.message
        },
        success(me, data) {
          me.$emit("callback", true)
        }
      })
    }
  },
  created() {
    this.getBankList()
  },
  components: { TaskbackModel }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
